﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Common;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Factories;
using FarseerPhysics.Dynamics.Contacts;

namespace chemicall
{
    class Agent : dynamicEntity
    {
        /*--CONSTS--*/
        public const int DEFAULT_SPEED = 100;

        private double distance;
        private bool inMoveRight = true;
        private bool grounded;
        public bool stable = false;


        //private Point screenCenter;
        protected State currentState = State.Idle;

        private Vector2 startingPosition;

        private SoundManager soundMan;

        public Agent(/*Point screenCenter,*/ double distance, SoundManager sM)
        {
            //this.screenCenter = screenCenter;
            this.distance = distance;
            soundMan = sM;
            speed.X = DEFAULT_SPEED;
        }

        public void LoadContent(ContentManager contentManager, string assetName, World world, Vector2 position, Nullable<Rectangle> entitySize, Nullable<Rectangle> spriteSize, Sprite id)
        {
            base.LoadContent(contentManager, world, assetName, position, entitySize, spriteSize, id);
            startingPosition = position;
            bodyFixture.OnCollision += MyOnCollision;
            stable = false;
        }

        public void Update(GameTime gameTime)
        {

                Vector2 pos = body.Position;
                Position.X = pos.X;
                Position.Y = pos.Y;

                if (grounded)
                    UpdateMovement();


                base.Update(gameTime);

                body.SetTransform(new Vector2(Position.X, Position.Y), 0.0f);

                updateAnimation();
            
        }


        private void UpdateMovement()
        {
            double curr_Dist = Position.X - startingPosition.X;
            if (inMoveRight)
            {
                direction.X = CONSTANTES.MOVE_RIGHT;
                if (curr_Dist <= distance)
                    currentState = State.Walking;
                else
                    inMoveRight = false;
            }
            else
            {
                direction.X = CONSTANTES.MOVE_LEFT;
                if (curr_Dist > 0)
                    currentState = State.Walking;
                else
                    inMoveRight = true;
            }
        }

        public bool MyOnCollision(Fixture f1, Fixture f2, Contact contact)
        {
            if (f1.Body.UserData is StaticEntity || f2.Body.UserData is StaticEntity)
            {
                Vector2 pos = Position;
                Vector2 norm;
                FixedArray2<Vector2> pts;
                contact.GetWorldManifold(out norm, out pts);
                bool below = true;
                for (int j = 0; j < 2; j++)
                    below &= (pts[j].Y < pos.Y + entitySize.Height * 0.5 + 1f);

                if (below)
                    grounded = true;
                else
                    grounded = false;

                return true;
            }

            return false;
        }

        public State state
        {
            get { return currentState; }
        }

        private void updateAnimation()
        {
            if (body.UserData is Anion)
            {
                if ((body.UserData as Anion).stable == true)
                {
                    currAnim = 0;
                }
                else
                {
                    currAnim = 2;
                }
            }
  

            if (body.UserData is Cation )
            {
                if ((body.UserData as Cation).stable == true)
                {
                    currAnim = 0;
                }
                else
                {
                    currAnim = 1;
                }
            }
             

        }
    }
}
